package site.wetsion.employ.server.service;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import site.wetsion.employ.server.mapper.CompanyMapper;
import site.wetsion.employ.server.mapper.JobMapper;
import site.wetsion.employ.server.mapper.UserMapper;
import site.wetsion.employ.server.pojo.Condition;
import site.wetsion.employ.server.pojo.Job;
import site.wetsion.employ.server.pojo.JobRequestDetail;
import site.wetsion.employ.server.pojo.Location;
import site.wetsion.employ.server.util.Page;
import site.wetsion.employ.server.vo.JobVo;
@Service
public class JobService {
	
	private final static Logger log = LoggerFactory.getLogger(JobService.class);
	
	@Autowired
	private JobMapper jobMapper;
	
	@Autowired
	private CompanyMapper companyMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 插入职位信息包含职位详情
	 * @param vo
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean addJob(JobVo vo){
		Job job = vo.getJob();
		JobRequestDetail detail = vo.getJobDetail();
		try {
			jobMapper.insertJobDetail(detail);
			try {
				job.setJob_request_detail(detail.getId());
				jobMapper.insertJob(job);
			} catch (Exception e) {
				log.error("插入公司失败，回滚", e);
				throw new Exception();
			}
			return true;
		} catch (Exception e) {
			log.error("插入职位详情失败，回滚", e);
			return false;
		}
	}
	/** 根据id获取职位*/
	public Job getJobById(BigInteger id){
		try {
			return jobMapper.getJobById(id);
		} catch (Exception e) {
			log.error("根据id获取职位失败，id="+id, e);
			return null;
		}
	}
	/** 根据id获取职位*/
	public JobRequestDetail getJobDetailById(BigInteger id){
		try {
			return jobMapper.getJobDetailById(id);
		} catch (Exception e) {
			log.error("根据id获取职位详情失败，id="+id, e);
			return null;
		}
	}
	/** 根据id删除职位*/
	public boolean deleteJobById(BigInteger id){
		try {
			jobMapper.deleteJobById(id);
			return true;
		} catch (Exception e) {
			log.error("删除职位失败，id="+id, e);
			return false;
		}
	}
	
	/** 获取所有职位信息，分页显示*/
	public List<Job> getAllJobByPage(Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Job> jobs = jobMapper.getJobsByPage((pageNum-1)*pageSize, pageSize);
			for (Job job : jobs) {
				JobRequestDetail detail = jobMapper.getJobDetailById(job.getJob_request_detail());
				job.setRequest_detail_content(detail.getRequest_content());
				String location_name = companyMapper.getLocationNameById(job.getJob_location());
				job.setLocation_name(location_name);
				String company_name = companyMapper.getCompanyById(job.getJob_company_id())
						.getCompany_name();
				job.setJob_company_name(company_name);
				String creater_name = userMapper.getCreaterNameById(job.getJob_creater_id());
				job.setCreater_name(creater_name);
				String domain_name = companyMapper.getDomainNameById(job.getJob_domain_id());
				job.setDomain_name(domain_name);
			}
			return jobs;
		} catch (Exception e) {
			log.error("获取职位列表失败，页码为"+pageNum, e);
			return null;
		}
	}
	/** 获取所有职位的个数*/
	public int getJobCount(){
		try {
			return jobMapper.getJobCount();
		} catch (Exception e) {
			log.error("获取所有职位个数失败", e);
			return 0;
		}
	}
	
	/** 根据公司id获取公司下所有招聘职位信息，分页显示*/
	public List<Job> getJobsOfCompanyByPage(Page page, BigInteger companyId){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Job> jobs = jobMapper.getJobsOfCompanyByPage((pageNum-1)*pageSize, pageSize, companyId);
			for (Job job : jobs) {
				String location_name = companyMapper.getLocationNameById(job.getJob_location());
				job.setLocation_name(location_name);
				String domain_name = companyMapper.getDomainNameById(job.getJob_domain_id());
				job.setDomain_name(domain_name);
			}
			return jobs;
		} catch (Exception e) {
			log.error("根据公司id获取职位列表失败，页码为"+pageNum+"，公司id="+companyId, e);
			return null;
		}
	}
	/** 获取所有职位的个数*/
	public int getJobCountOfCompany(BigInteger companyId){
		try {
			return jobMapper.getCountOfJobsOfCompanyByPage(companyId);
		} catch (Exception e) {
			log.error("获取公司下职位个数失败，公司id="+companyId, e);
			return 0;
		}
	}
	
	
	public boolean updateJob(Job job) {
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		job.setGmt_modified(timestamp);
		try {
			jobMapper.updateJob(job);
			return true;
		} catch (Exception e) {
			log.error("更新职位失败，id="+job.getId(), e);
			return false;
		}
	}
	public boolean updateJobDetail(JobRequestDetail detail) {
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		detail.setGmt_modified(timestamp);
		try {
			jobMapper.updateJobDetail(detail);
			return true;
		} catch (Exception e) {
			log.error("更新职位详情失败，id="+detail.getId(), e);
			return false;
		}
	}
	
	/**
	 * 根据职位名称名称查询职位，并分页
	 * @param page
	 * @param name 查询的关键字
	 * @return
	 */
	public List<Job> searchJobByName(Page page, String name){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		System.out.println("name="+name);
		try {
			List<Job> jobs = jobMapper.getJobOnSearchNameByPage((pageNum-1)*pageSize, pageSize, name);
			for (Job job : jobs) {
				String location_name = companyMapper.getLocationNameById(job.getJob_location());
				job.setLocation_name(location_name);
				String domain_name = companyMapper.getDomainNameById(job.getJob_domain_id());
				job.setDomain_name(domain_name);
			}
			return jobs;
		} catch (Exception e) {
			log.error("分页根据名称查询职位出错，查询关键字是"+name, e);
			return null;
		}
	}
	/**
	 * 获取符合关键字的记录数量
	 * @param name 关键字
	 * @return
	 */
	public int getSearchCountByName(String name){
		try {
			int count = jobMapper.getSearchCountByName(name);
			return count;
		} catch (Exception e) {
			log.error("安关键字"+name+"查询出错", e);
			return 0;
		}
	}
	public List<Job> searchJobByCondition(Page page, Condition condition) {
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Job> jobs = jobMapper.getJobOnConditionByPage(
					(pageNum-1)*pageSize, pageSize, 
					condition.getKeyword(), 
					condition.getLocation(), 
					condition.getDomain(), 
					condition.getJobCategory(), 
					condition.getEduBackground(), 
					condition.getWorktime());
			for (Job job : jobs) {
				String location_name = companyMapper.getLocationNameById(job.getJob_location());
				job.setLocation_name(location_name);
				String domain_name = companyMapper.getDomainNameById(job.getJob_domain_id());
				job.setDomain_name(domain_name);
			}
			return jobs;
		} catch (Exception e) {
			log.error("分页条件查询职位出错", e);
			return null;
		}
	}
	public int getSearchCountByCondition(Condition condition) {
		try {
			int count = jobMapper.getSearchCountByCondition(
					condition.getKeyword(), 
					condition.getLocation(), 
					condition.getDomain(), 
					condition.getJobCategory(), 
					condition.getEduBackground(), 
					condition.getWorktime());
			return count;
		} catch (Exception e) {
			log.error("安条件查询出错", e);
			return 0;
		}
	}
	
	/** 根据发布人id获取有效职位*/
	public List<Job> getValidJobListByUid(BigInteger userId, Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Job> jobs = jobMapper.getValidJobListByCreaterId((pageNum-1)*pageSize, pageSize, userId);
			for (Job job : jobs) {
				String location_name = companyMapper.getLocationNameById(job.getJob_location());
				job.setLocation_name(location_name);
				String domain_name = companyMapper.getDomainNameById(job.getJob_domain_id());
				job.setDomain_name(domain_name);
			}
			return jobs;
		} catch (Exception e) {
			log.error("根据发布人id获取有效职位失败", e);
			return null;
		}
		
	}
	public int getCountOfValidJobByCreaterId(BigInteger userId){
		try {
			return jobMapper.getCountOfValidJobByCreaterId(userId);
		} catch (Exception e) {
			return 0;
		}
	}
	/** 根据发布人id获取失效职位*/
	public List<Job> getNulityJobListByUid(BigInteger userId, Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			List<Job> jobs = jobMapper.getNulityJobListByCreaterId((pageNum-1)*pageSize, pageSize, userId);
			for (Job job : jobs) {
				String location_name = companyMapper.getLocationNameById(job.getJob_location());
				job.setLocation_name(location_name);
				String domain_name = companyMapper.getDomainNameById(job.getJob_domain_id());
				job.setDomain_name(domain_name);
			}
			return jobs;
		} catch (Exception e) {
			log.error("根据发布人id获取失效职位失败", e);
			return null;
		}
	}
	public int getCountOfNulityJobByCreaterId(BigInteger userId){
		try {
			return jobMapper.getCountOfNulityJobByCreaterId(userId);
		} catch (Exception e) {
			return 0;
		}
	}
	
	/** 根据关键词搜索城市*/
	public List<Location> searchLocationByKeyword(String keyword){
		try {
			return  jobMapper.getLocationsByKeyword(keyword);
		} catch (Exception e) {
			log.error("根据关键词获取城市失败，keyword：{},{}", keyword,e);
			return null;
		}
	}
	
}
